game = {}
QMPlugin = game

function game.init()
    mlog.info("game init >>>>>>>>>>>")
    game.curLine = 0
    game.paraConfig = {
        ["切线按钮"] = {941,1630,1048,1673,"F0F3F0","-45|7|EAF6F4,-36|15|E7F4F1,-17|12|EFF2EE,-8|0|F7F8F6,-13|10|F4F6F3",0,0.9},
        ["线1"] = {433,1365,1042,1673,"C9D5D6","3|3|C7D4D5,-6|3|E3ECEE,-15|3|E3ECEE,1|-49|6EC572,-6|-50|6AC16E,-15|-51|63BC68",0,0.9},
        ["线2"] = {433,1365,1042,1673,"C4CFD0","4|6|B6C0C2,-4|6|D6DFE1,-14|1|E1EBEC,-15|7|B7C3C5,4|-48|6FC673,-10|-48|65BE69",0,0.9},
        ["线3"] = {433,1365,1042,1673,"CBD6D7","7|-3|CCD7D8,-7|-3|CDD8D9,-4|5|D0DBDC,10|-52|6EC673,-7|-49|64BD68,107|-51|65BE69",0,0.9},
        ["线4"] = {433,1365,1042,1673,"E2EBEC","12|7|F2FAFC,4|7|ECF5F7,0|10|C3CFCF,12|-47|6EC571,-3|-46|64BC68,224|-47|65BE69",0,0.9},
        ["线5"] = {433,1365,1042,1673,"C9D4D4","8|0|C9D4D4,-3|8|D3DDDE,-9|2|D5DFDF,8|-46|6FC673,-6|-50|65BD69,331|-48|65BE69",0,0.9},
        ["活动旗子按钮"] = {975,1331,1075,1439,"D1F3FF","11|-13|E4F8FF,-13|11|BAEDFF,-13|-4|BCECFF,-14|-14|B9EBFE,19|-1|EEFAFE",0,0.9},
        ["活动组队按钮未选中"] = {531,1775,819,1912,"555CB9","22|-11|95BEF5,40|5|95BEF5,9|6|95BEF5,-13|7|95BEF5,-34|-14|95BEF5,-55|-11|545BB9,-45|-28|545BB9",0,0.9},
        ["任务面板日常任务未打开"] = {70,236,1073,678,"8FCCFB","16|-55|182A72,-10|-42|182A72,12|-11|182A72,-7|-4|182A72,4|20|182A72,7|131|5277B8,-3|140|5075B6,9|152|4F74B5,7|54|182A72",0,0.9},
        ["任务面板日常抓鬼任务按钮"] = {64,181,1009,713,"B4E2FF","0|-64|192A72,-7|-62|192A72,7|-25|192A72,9|-5|1A2C73,-7|-13|192A72,2|19|192A72,1|55|B4E2FF,11|12|B4E2FF,15|44|192A72",0,0.9},
        ["任务面板日常抓鬼立即前往按钮"] = {57,1365,230,1656,"4D56D7","6|-95|5059DA,4|100|4F58D9,20|-9|D1F0FF,4|-69|D1F0FF,-7|47|D1F0FF,-12|65|D1F0FF,16|64|D1F0FF",0,0.9},
        ["组队队长标记"] = {696,28,883,122,"8DBBEB","33|-6|182A71,26|-10|182A71,12|-10|182A71,-16|-5|182A71,-39|2|8CBBEB,-16|7|182A71,10|14|1D2F76",0,0.9},
        ["任务面板日常抓鬼关闭按钮"] = { 856,1625,1022,1812,"B1F8FF","4|-31|3E468B,41|-2|3D468C,2|31|484D92,-33|-6|4F5195,-75|-8|5147E2",0,0.9},
        ["组队面板创建队伍按钮"] = {34,1311,162,1595,"D1F0FF","1|-82|515ADB,-27|4|444EDE,16|110|5C64E6,11|67|D1F0FF,-17|70|D1F0FF,9|-3|D1F0FF,10|-31|D1F0FF",0,0.9},
        ["摆摊按钮未选中"] = {215,1776,738,1918,"57A0F5","-66|-2|728FEA,62|-5|347BE6,32|0|182971,30|-14|182971,-44|-18|182972,-45|12|182972,-18|13|182972,10|11|182972,36|14|182971",0,0.9},
        ["关注物品未选中"] = {886,594,1023,1014,"799BC6","14|-60|182971,-1|-47|182971,-13|-21|182971,-13|11|182971,16|11|182971,10|35|182971,15|46|182971,0|71|182971,-12|71|182971",0,0.9},
        ["关注按钮未选中"] = {32,32,967,496,"8FCCFB","16|-103|3762EC,-7|-84|3862ED,14|-70|3862ED,16|-27|192A72,-10|-34|182A72,-9|-7|182A72,-10|0|182A72,18|17|182A72,14|28|182A72",0,0.9},
        ["商城按钮"] = {970,1431,1074,1550,"BBF1FE","-12|-8|AAE8FF,1|-10|C1EFFF,0|-16|BDEDFF,-14|-17|A9E6FF,8|-23|C4E9F6,20|-17|E3F9FF,30|0|F1FBFF",0,0.9},
        ["背包按钮"] = {707,1678,834,1799,"2B4D50-303030","-27|-11|E6F3F3,-46|-17|CBD6DA,-36|1|EFFAFA,-47|-3|E0EBEC,-27|11|F0F5F7,-46|10|CDD2D3,-47|27|E3E5E6,-2|14|B5E2F9,-1|24|3D7C7F",2,0.95},
        ["快捷组队按钮"] = {15,1599,175,1907,"4D56D8","1|98|4D56D8,4|-109|4F59DA,-10|-54|D1F0FF,-12|-40|D1F0FF,18|-15|D1F0FF,-13|-3|D1F0FF,16|8|D1F0FF,-12|29|D1F0FF,0|42|D1F0FF",0,0.9},
    }
    game.closeBtnConfig = {
        ["充值礼包活动关闭按钮"] = {788,1507,914,1646,"81C4F2","17|-14|71B4F6,-13|-14|92D3EB,-24|0|3E4881,-1|24|3F4983,21|-5|3C4780",0,0.9},
        ["组队关闭按钮"] = {975,1797,1053,1873,"C6EEFB","11|-11|CDF0FE,-9|-10|C0EBF8,-11|10|BEEBF7,10|10|CDF1FD,-5|-6|C5ECF9,6|-7|CAEDFB,-4|5|C2EAFA,5|4|C9EFFC",0,0.95},
        ["通用关闭按钮"] = {35,1123,1061,1901,"ADF5FF","16|-22|81C8FE,19|19|8AD4FF,-20|16|C1FFFF,-17|-21|B5FEFE,40|-3|3D468B,5|-35|3D468B,-35|-2|505195,3|39|4C5195,-62|-4|778042",0,0.9},
        ["摆摊关闭按钮"] = { 957,1770,1079,1913,"88CEFC","18|-23|73B8FB,-21|20|A1E8FE,24|19|6AA0EC,-17|-21|9EE4FE,0|-39|394179,-40|-5|434283,42|-5|384079,1|36|414381",0,0.9},
        ["摆摊宠物关闭按钮"] = { 808,1716,923,1830,"92DDFF","14|-17|7AC7FF,-15|-16|A2ECFF,-17|10|9DE9FE,16|12|7CC7FE,34|-6|445493,5|-31|3D4C8C,-35|0|605796,1|32|455293",0,0.9},
        ["便捷组队关闭按钮"] = {  898,1597,1048,1738,"91DDFF","17|-15|7CC8FF,-14|17|A2ECFE,-1|-24|435294,2|33|435294,-33|0|595794,31|3|445596,-8|-6|99E4FE",0,0.9},
        ["活动任务关闭按钮"] = {  923,1795,1079,1918,"ABF3FF-303030","21|-17|83CCFF,-19|23|C3FFFF,-20|-18|B5FDFE,19|22|8BD5FE,43|0|3D468B,2|-39|3E468B,-1|36|4C5093,-76|-6|5248E2,-60|1|8A924C",0,0.9},
        ["任务面板关闭按钮"] = {   849,1623,1032,1820,"ADF5FF-303030","17|-21|83CBFE,-18|21|C2FEFE,20|16|88D3FF,-17|-20|B5FDFF,47|-4|3D468A,3|-38|3D468C,7|33|484D91,-61|-1|848C47,-74|-6|5147E2",0,0.9},
        ["跑商货品界面关闭按钮"] = {   861,1781,1028,1913,"91DDFF-303030","17|-14|7CC8FF,14|18|7BC5FF,-13|15|A1ECFF,-16|-19|A4ECFF,-1|-33|445392,30|0|455597,4|29|3F4E8D,-30|2|4D5495",0,0.9},
    }
end

-- 切换路线
function game.switchLine()
    game.curLine = game.curLine + 1
    if game.curLine > 5 then
        game.curLine = 1
    end
    local para = game.paraConfig["切线按钮"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false
    else
        game.tap(itemX, itemY)
        game.delay(500)
        para = game.paraConfig["线"..tostring(game.curLine)]
        itemX,itemY = game.findMultiColor(utils.unpack(para))
        if itemX == -1 or itemY == - 1 then
            return false
        else
            game.tap(itemX, itemY)
            mlog.info("切换到线路："..tostring(game.curLine))
            return true
        end
    end

    return false
end

function game.OtherPanelClose()
    for k,para in pairs(game.closeBtnConfig) do
        local itemX,itemY = game.findMultiColor(utils.unpack(para))
        if itemX > -1 and itemY > - 1 then
            game.tap(itemX, itemY)
            game.delay(500)
        end
    end
end

--摆摊按钮未选中
function game.baitanBtnUnSelected()
    local para = game.paraConfig["摆摊按钮未选中"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

--关注物品未选中
function game.followGoodsUnSelected()
    local para = game.paraConfig["关注物品未选中"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

--打开关注面板
function game.openFollowPanel()
    local shangchengBtn,x,y = game.shangchengBtn()
    if shangchengBtn then
        mlog.info("openFollowPanel 找到商城按钮")
        game.tap(x,y)
        game.delay(5000)
    end

    local baitanBrn,x,y = game.baitanBtnUnSelected()
    if baitanBrn then
        mlog.info("openFollowPanel 找到摆摊按钮")
        game.tap(x,y)
        game.delay(1000)
    end

    local followGoodsBrn,x,y = game.followGoodsUnSelected()
    if followGoodsBrn then
        mlog.info("openFollowPanel 找到关注页签")
        game.tap(x,y)
        game.delay(1000)
    end

    local followBtn,x,y = game.followBtnUnSelected()
    if followBtn then
        mlog.info("openFollowPanel 找关注按钮")
        game.tap(x,y)
    end
end

--关注按钮未选中
function game.followBtnUnSelected()
    local para = game.paraConfig["关注按钮未选中"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

--商城按钮
function game.shangchengBtn()
    local para = game.paraConfig["商城按钮"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

--背包按钮
function game.beibaoBtn()
    local para = game.paraConfig["背包按钮"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

--快捷组队按钮
function game.fastTeamBtn()
    local para = game.paraConfig["快捷组队按钮"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

function game.drag(sx, sy, tx, ty, time)
    time = time or 1000
    LuaAuxLib.TouchOperation(0, sx, sy, 1)
    game.delay(200)
    LuaAuxLib.TouchOperation(1, tx, ty, 1, time)
    game.delay(200)
    LuaAuxLib.TouchOperation(2, tx, ty, 1)
    game.delay(200)
end

function game.IsDisplayDead(delay)
    delay = delay or 5000
    local txt1 = LuaAuxLib.SmartOcr(866,1816,903,1919,"F5FDFF-303030")
    game.delay(delay)
    local txt2 = LuaAuxLib.SmartOcr(866,1816,903,1919,"F5FDFF-303030")
--    local isDead = zm.IsDisplayDead(854,1691,1029,1918, delay)
    if txt1 == txt2 then
        mlog.info("卡屏了")
        return true
    else
        mlog.info("没卡屏")
        return false
    end
end

-- 是否功能栏开启
function game.IsfunPanelClose()
    local para = {887,0,939,52,"BFE2EF","-12|-11|B8DBE8,13|-14|C6E8F3,6|-5|C6E5F3,-6|-6|B8DDE8",0,0.95}
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

-- 活动旗子按钮
function game.CheckCampainFlagBtn()
    local para = game.paraConfig["活动旗子按钮"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

-- 获取关闭按钮
function game.GetCloseBtn(key)
    local para = game.closeBtnConfig[key]
    if not para then
        return -1,-1
    end

    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

-- 获取关闭按钮
function game.CloseBtnByKey(key)
    local para = game.closeBtnConfig[key]
    if not para then
        return false
    end

    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false
    else
        game.tap(itemX,itemY)
        return true
    end
end

-- 任务面板日常任务未打开
function game.CheckDailyTaskBtn()
    local rtn1,rtn2,rtn3 = utils.retry(function()
        local para = game.paraConfig["任务面板日常任务未打开"]
        local itemX,itemY = game.findMultiColor(utils.unpack(para))
        if itemX == -1 or itemY == - 1 then
            return false,itemX,itemY
        else
            return true,itemX,itemY
        end
    end,6,500)
    return rtn1,rtn2,rtn3
end


-- 组队队长标记
function game.CheckTeamLeaderFlag()
    local para = game.paraConfig["组队队长标记"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

-- 任务面板日常抓鬼任务按钮
function game.CheckDailyTaskZhuaguiBtn()
    local para = game.paraConfig["任务面板日常抓鬼任务按钮"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

-- 任务面板日常抓鬼立即前往按钮
function game.CheckDailyTaskZhuaguiGoBtn()
    local para = game.paraConfig["任务面板日常抓鬼立即前往按钮"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

-- 任务面板日常抓鬼关闭按钮
function game.CheckDailyTaskZhuaguiCloseBtn()
    local para = game.paraConfig["任务面板日常抓鬼关闭按钮"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

-- 组队面板创建队伍按钮
function game.CheckTeamPanelCreateBtn()
    local para = game.paraConfig["组队面板创建队伍按钮"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

-- 点击领取的抓鬼任务前往任务
function game.ClickTaskPanelZhuaguiBtn()
    local panelShow = game.OpenTaskPanel()
    if not panelShow then
        mlog.info("任务面板没打开")
    end
    mlog.info("任务面板已打开")
    game.delay(800)
    local dailyTaskBtn,x,y = game.CheckDailyTaskBtn()
    if dailyTaskBtn then
        game.tap(x, y)
        game.delay(500)
        local zhuaguikBtn,x,y = game.CheckDailyTaskZhuaguiBtn()
        if zhuaguikBtn then
            game.tap(x, y)
            game.delay(500)
            local gokBtn,x,y = game.CheckDailyTaskZhuaguiGoBtn()
            if gokBtn then
                game.tap(x, y)
                return true
            end
        end
    end

    local closeBtn,x,y = game.CheckDailyTaskZhuaguiCloseBtn()
    if closeBtn then
        game.tap(x, y)
    end

    return false
end

-- 活动组队按钮未选中
function game.FindCampainTeamBtn()
    local para = game.paraConfig["活动组队按钮未选中"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

-- 活动旗子按钮
function game.OpenCampainPanel()
    local panelShow = utils.retry(function()
        local show,x,y = game.CheckCampainFlagBtn()
        if show then
            game.tap(x, y)
            return true
        end
        return false
    end,3,500)

    return panelShow

end

-- 开启功能栏
function game.OpenfunPanel()
    local show,x,y = game.IsfunPanelClose()
    if show then
        game.tap(x, y)
    end
    return show
end

-- 队伍界面是否打开
function game.CheckTeamPanelOpen()
    local para = {939,59,1065,391,"40539F","5|-29|AFE0FE,-19|18|AADEFE,-23|-19|40529F,-10|-275|A3D8F8,5|-224|182971,8|-160|182971,-22|-156|182971",0,0.9}
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

-- 开启队伍界面
function game.OpenTeamPanel()
    local show,x,y = game.FunTeamBtnSelected()
    mlog.info("OpenTeamPanel >>>>>>>>>>>>>>>> "..tostring(show))
    if show then
        game.tap(x, y)
        return true
    end
    local show,x,y = game.FunTeamBtnUnSelected()
    if show then
        game.tap(x, y)
        game.delay(800)
        local show,x,y = game.FunTeamBtnSelected()
        if show then
            game.tap(x, y)
            return true
        end
    end

    return false
end

-- 开启任务界面
function game.OpenTaskPanel()
    local show,x,y = game.FunTaskBtnSelected()
    if show then
        game.tap(x, y)
        return true
    end
    local show,x,y = game.FunTaskBtnUnSelected()
    if show then
        game.tap(x, y)
        game.delay(800)
        local show,x,y = game.FunTaskBtnSelected()
        if show then
            game.tap(x, y)
            return true
        end
    end

    return false
end

-- 队伍按钮选中
function game.FunTeamBtnSelected()
    local para = {861,12,983,305,"101D52","6|-15|0E1B51,-5|-30|0E1B51,1|-216|BDDFE6,-8|-205|C2E1E8,12|-206|B7D7DF,12|-15|0E1B51,12|18|0E1B50",0,0.9}
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

-- 队伍按钮未选中
function game.FunTeamBtnUnSelected()
    local para = { 861,0,967,323,"F4FCFE","1|-31|F5FDFF,9|11|F5FDFF,-5|-109|0E1B50,-12|-136|0E1B51,5|-136|0E1B51,1|-216|BFE0EA,-10|-208|C5E2EC,9|-207|BDDCE5",0,0.9}
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

-- 任务按钮选中
function game.FunTaskBtnSelected()
    local para = { 841,7,980,339,"0E1B50","0|-79|BEDFE9,-13|-70|C2E1ED,9|-68|B6D5E3,-5|20|0E1B50,9|31|0E1B50,3|136|F4FBFD,-8|106|F5FDFF,9|149|F5FDFF,-12|153|F5FDFF",3,0.9}
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

-- 任务按钮未选中
function game.FunTaskBtnUnSelected()
    local para = {859,3,1005,316,"F5FDFF","7|-37|F5FDFF,15|8|F5FDFF,-2|12|F4FDFF,7|123|0E1B51,6|131|0E1B51,5|-103|BCE0EE,-6|-93|C2E3F1,16|-94|B7DCEB",0,0.9}
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

-- 组队关闭按钮
function game.FindTeamClosePanelBtn()
    local rtn1,rtn2,rtn3 = utils.retry(function()
        local para = {950,1778,1063,1912,"C6EEFB","-10|-11|BEEBF7,12|-12|CEEFFF,9|10|CDF1FD,-11|11|BEEBF7,-118|28|3945A7,-150|28|ADD3EF,-206|27|C9ECFF",0,0.95}
        local itemX,itemY = game.findMultiColor(utils.unpack(para))
        if itemX == -1 or itemY == - 1 then
            return false,itemX,itemY
        else
            return true,itemX,itemY
        end
    end,2,500)
    return rtn1,rtn2,rtn3
end

function game.IsFuben()
    local itemX,itemY = game.findMultiColor( 757,1437,883,1549,"C6EDFA","2|22|CAEEFE,18|13|CFF1FF,-9|15|C3EBF9,-24|8|EFF6F8,-38|-6|E8F6F4,-23|8|EDF5F6,-25|26|E3F2EF",0,0.95)
    if itemX == -1 or itemY == - 1 then
        return false
    else
        return true
    end
end

-- 等待寻路结束
function game.WaitFindWayFinish(timeOut)
    timeOut = timeOut or 10000
    local start = os.clock()

    while not game.IsDisplayDead(2000) do
       game.delay(500)
        local execTime = (os.clock() - start) * 1000
        if execTime >= timeOut then
            mlog.info("检测寻路超时 timeOut = ", timeOut)
            return false
        end
    end

    mlog.info("检测寻路结束")
    return true
end

function game.cmpColorEx(...)
    local cmp = LuaAuxLib.CmpColorEx(...)
    if cmp > - 1 then
        return true
    end
    return false
end

function game.cmpColor(...)
    local cmp = LuaAuxLib.CmpColor(...)
    if cmp > - 1 then
        return true
    end
    return false
end

function game.findMultiColor (...)
    local intX, intY = LuaAuxLib.FindMultiColor(...)
    return intX, intY
end

function game.tap(x, y)
    zm.SetTap({minx=-5, maxx=5, miny=-5, maxy=5,mintouch=10,maxtouch=30,showlog="显示"})
    zm.Tap(x, y)
end

function game.realTap(x, y)
    zm.SetTap({showlog="显示"})
    zm.Tap(x, y)
end

function game.delay(...)
    zm.Delay(...)
end